package com.ebocy.hammer.core.db.model;

import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.ebocy.hammer.core.db.IEntity;
import com.ebocy.hammer.core.db.annotations.Id;
public class BaseEntity implements IEntity{
	@Id
	private String id;

	@Override
	public void setId(String id) {
		this.id=id;
	}

	@Override
	public String getId() {
		return this.id;
	}
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}
	@Override
	public int hashCode() {
		return super.hashCode();
	}
	@Override
	public int compareTo(IEntity obj) {
		int compare = -1;

		if (obj == null)
			compare = -1;
		else if (this == obj)
			compare = 0;
		else if (!(obj instanceof BaseEntity))
			compare = -1;
		else if (!this.getClass().equals(obj.getClass()))
			compare = -1;
		else {
			BaseEntity castObj = (BaseEntity) obj;
			CompareToBuilder builder = new CompareToBuilder();
			builder.append(this.getId(), castObj.getId());
			compare = builder.toComparison();
		}

		return compare;
	}

	public boolean equals(Object obj) {
		boolean isEqual = false;

		if (obj == null) {
			isEqual = false;
		} else if (this == obj) {
			isEqual = true;
		} else if (!(obj instanceof BaseEntity)) {
			isEqual = false;
		} else if (!this.getClass().equals(obj.getClass())) {
			isEqual = false;
		} else {
			BaseEntity castObj = (BaseEntity) obj;
			isEqual=this.getId().equals(castObj.getId());
		}

		return isEqual;
	}
}
